<template>
  <div>
    <Nav message="-1" />
    <van-address-edit
      :area-list="areaList"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      save-button-text="修改"
      :address-info="addressInfo"
    />
  </div>
</template>
<script>
import AreaList from "../api/area";
import { Toast } from "vant";
import Nav from "@/components/Nav.vue";
export default {
  components: {
    Nav,
  },
  //获取传来的值，填入addressInfo
  created() {
    // let data = JSON.parse(this.$route.query.item);
    // let index = data.address.indexOf("区");
    // if (index < 0) index = data.address.indexOf("县");
    // this.addressInfo.addressDetail = data.address.substring(index + 1); //截取

    let skuId = this.$route.query.skuId;
    let skuNum = this.$route.query.skuNum;
    this.detailGoAddressSkuId = skuId;
    this.detailGoAddressSkuNum = skuNum;

    let hasFlagAddress = this.decodeUnicode(this.$route.query.address);
    let flagIndex = hasFlagAddress.indexOf("&");
    let substringOkAddress = hasFlagAddress.substring(flagIndex + 1);
    console.log(hasFlagAddress);

    this.addressInfo.addressDetail = substringOkAddress;
    this.addressInfo.areaCode = this.$route.query.areaCode;
    this.addressInfo.name = this.$route.query.name;
    this.addressInfo.tel = this.$route.query.tel;
    this.addressInfo.id = this.$route.query.id;
  },
  data() {
    return {
      areaList: AreaList,
      addressInfo: {
        id: "1",
        areaCode: "330106",
        name: "Anni",
        tel: "13000000000",
        addressDetail:
          "浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室",
        token: null,
        // isDefault: true,
      },
      detailGoAddressSkuId: null, //详情页跳地址
      detailGoAddressSkuNum: null, //详情页跳地址
    };
  },
  methods: {
    onSave(item) {
      console.log(item)
      // console.log("this.addressInfo：");
      // console.log(this.addressInfo);

      let isShopToken = this.$cookie.get("shoptoken");
      if (isShopToken == null) {
        this.$router.push({
          path: "/login",
          query: { item: this.$route.path },
        });
      }

      this.addressInfo.address =
        item.country + item.province + item.city + item.county + "&" + item.addressDetail;
      this.addressInfo.name = item.name;
      this.addressInfo.tel = item.tel;

      this.addressInfo.token = isShopToken;
      const _this = this;
      var editAddressJSON = JSON.stringify(_this.addressInfo);
      axios
        .post(this.$api + "member/editBuyerAddress", editAddressJSON, {
          headers: {
            "Content-Type": "application/json;charset=UTF-8",
          },
        })
        .then(function (resp) {
          console.log(resp);
          if (resp.data.code == 0) {
            let instance = Toast("修改成功");
            setTimeout(() => {
              instance.close();
              _this.$router.push({
                path: "/addressList",
                query: {
                  skuId: _this.detailGoAddressSkuId,
                  skuNum: _this.detailGoAddressSkuNum,
                },
              });
            }, 1000);
          }
        });
    },
    // 解码
    decodeUnicode(str) {
      str = str.replace(/\\/g, "%");
      //转换中文
      str = unescape(str);
      //将其他受影响的转换回原来
      str = str.replace(/%/g, "\\");
      //对网址的链接进行处理
      str = str.replace(/\\/g, "");
      return str;
    },
  },
};
</script>
<style scope>
.van-button {
  background: rosybrown;
  border: rosybrown;
  font-weight: bold;
}
</style>